<?php
class Mnews extends CI_Model 
{
	public $table = 'tbl_news';
	public $table2 ='tbl_news_cat';
	public $pri_key="news_id";
	function __construct()
	{
		parent::__construct();
	}
	//****************** name cat ***************************//
	function namecat($catID)
	{
		$this->db->where('cat_id',$catID);
		$namecat = $this->db->get($this->table2);
		if($namecat != "")
		{
			$name = $namecat->row_array();
			return $name['cat_name'];
		}
	}
	//******************  news    *************************//
	function del_img($id)
	{
		$data = array('news_image'=>"");
		$this->db->where('news_id',$id);
		if($this->db->update($this->table,$data))
			return true;
		return false;
	}
	//******************* news_cat *************************//
	function news_catmenu()
	{
		$this->db->where("cat_status",1);
		$this->db->order_by("cat_id","DESC");
		$this->db->limit(6);
		$query = $this->db->get('tbl_news_cat');
		if(!empty($query))
		{

			return $query->result_array();
		}
		else
			return false;
	}
	
	//******************* news *****************************//
	function news_index($type="")
	{
		$this->db->where("news_status",1);
		if($type != "")
			$this->db->where("news_new",$type);
		$query = $this->db->get('tbl_news');
		if(!empty($query))
		{

			return $query->result_array();
		}
		else
			return false;
	}
	function get_catnew($catid)
	{
		$this->db->where('cat_id',$catid);
		$this->db->where('news_status',1);
		$this->db->limit(10);
		$query = $this->db->get('tbl_news');
		if(!empty($query))
			return $query->	result_array();
	}
	//****************** New 2 cap *********************//
	function news_indexcat2($type="")
	{
		$this->db->where("cat_status",1);
		$this->db->order_by("cat_id","DESC");
		$this->db->limit(4);
		$query = $this->db->get('tbl_news_cat');
		if(!empty($query))
		{
			$data = $query->result_array();
			$data = $this->news_indexsub($data,$type);
			return $data;
		}
		else
			return false;
	}
	function news_indexsub($data,$type="")
	{
		if(!empty($data))
		{
			$count = 0; 
			foreach($data as $data_cat)
			{
				$this->db->where("cat_id",$data_cat['cat_id']);
				if($type != "")
					$this->db->where("news_new",$type);
				$this->db->where("news_status",1);
				$this->db->order_by("news_id","DESC");
				$data[$count++]['news_sub'] = $this->db->get('tbl_news')->result_array();
			}	
			
		}
		return $data;
	}
	 // *********************** NEWS search *****************************//
	 function newsearch($code,$keyword,$demand,$tower,$type,$furniture,$Bedrooms,$floors,$area,$price,$tygia,$pricetype,$page,$limit)
	 {
			 if(!empty($code))
			 	$this->db->like("news_code","like '%$code%'");
			 if(!empty($keyword))
			 {
				 if(empty($_SESSION['lang']))
			 		$this->db->like("news_name_en","like '%$keyword%'");
				else
					$this->db->like("news_name_en","like '%$keyword%'");
			 }
			 if(!empty($demand))
			 	$this->db->where("news_need",$demand);
			 if(!empty($tower))
			 	$this->db->where("type_id",$tower);
			 if(!empty($type))
			 	$this->db->where("news_type",$type);
			if(!empty($furniture))
			 	$this->db->where("news_furniture",$furniture);
			if(!empty($Bedrooms))
			{
			 	$this->db->where("news_num_room"." >= ",$Bedrooms[0]);
				$this->db->where("news_num_room"." <= ",$Bedrooms[1]);
			}
			
			if(!empty($floors))
			{
				$this->db->where("news_num_floor"." >=",$floors[0]);
				$this->db->where("news_num_floor"." <= ",$floors[1]);
			}
			if(!empty($area))
			{
				$this->db->where("news_area"." >= ",$area[0]);
				$this->db->where("news_area"." <= ",$area[1]);
			}
			if(!empty($price))
			{
				if($tygia == 1)
				{
					$this->db->where("news_price_usd"." >= ",$price[0]);
					$this->db->where("news_price_usd"." <= ",$price[1]);
				}
				else if($tygia == 2)
				{
					$this->db->where("news_price_sjc"." >= ",$price[0]);
					$this->db->where("news_price_sjc"." <= ",$price[1]);
				}
				else
				{
					$this->db->where("news_price_vnd"." >= ",$price[0]);
					$this->db->where("news_price_vnd"." <= ",$price[1]);
				}
			}
			if(!empty($pricetype))
			{
				$this->db->where("news_pricetype",$pricetype);
			}
			$this->db->limit($limit, $page);
			$this->db->where("news_status",'1');
			return $this->db->get("tbl_news")->result_array();
			 	
	 }
	public function count_all_search($code,$keyword,$demand,$tower,$type,$furniture,$Bedrooms,$floors,$area,$price,$tygia,$pricetype)
	{
			 if(!empty($code))
			 	$this->db->like("news_code","like '%$code%'");
			 if(!empty($keyword))
			 {
				 if(empty($_SESSION['lang']))
			 		$this->db->like("news_name_en","like '%$keyword%'");
				else
					$this->db->like("news_name_en","like '%$keyword%'");
			 }
			 if(!empty($demand))
			 	$this->db->where("news_need",$demand);
			 if(!empty($tower))
			 	$this->db->where("type_id",$tower);
			 if(!empty($type))
			 	$this->db->where("news_type",$type);
			if(!empty($furniture))
			 	$this->db->where("news_furniture",$furniture);
			if(!empty($Bedrooms))
			{
			 	$this->db->where("news_num_room"." >= ",$Bedrooms[0]);
				$this->db->where("news_num_room"." <= ",$Bedrooms[1]);
			}
			
			if(!empty($floors))
			{
				$this->db->where("news_num_floor"." >=",$floors[0]);
				$this->db->where("news_num_floor"." <= ",$floors[1]);
			}
			if(!empty($area))
			{
				$this->db->where("news_area"." >= ",$area[0]);
				$this->db->where("news_area"." <= ",$area[1]);
			}
			if(!empty($price))
			{
				if($tygia == 1)
				{
					$this->db->where("news_price_usd"." >= ",$price[0]);
					$this->db->where("news_price_usd"." <= ",$price[1]);
				}
				else if($tygia == 2)
				{
					$this->db->where("news_price_sjc"." >= ",$price[0]);
					$this->db->where("news_price_sjc"." <= ",$price[1]);
				}
				else
				{
					$this->db->where("news_price_vnd"." >= ",$price[0]);
					$this->db->where("news_price_vnd"." <= ",$price[1]);
				}
			}
			if(!empty($pricetype))
			{
				$this->db->where("news_pricetype",$pricetype);
			}
			$this->db->where("news_status",'1');
			return $this->db->count_all_results("tbl_news");
	}
	// lay chi tiet 
	public function info_detail($id,$table)
	{

		$this->db->where($this->pri_key, $id);
		return $this->db->get($table)->row_array();
	}

	function get_hot($lang)
	{
		$this->db->where('news_new',1);
		
		$this->db->where('news_lang',$lang);
			$this->db->where('news_status',1);
		$this->db->join("tbl_news_cat" , "cat_id = cat_id and cat_lang = $lang" );
		
		$query = $this->db->get('tbl_news');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	public function get_sub_cat ($lang)
	{
		$this->db->where('cat_lang',$lang);
			$this->db->where('cat_status',1);
		$query = $this->db->get('tbl_news_cat');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	public function get_sub_type($cat)
	{
		$this->db->where('cat_id',$cat);
		$this->db->where('type_status',1);
		$query = $this->db->get('tbl_news_type');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	

	public function get_news_detail($id,$lang="")
	{
		$this->db->where($this->pri_key,$id);
		$this->db->where('news_status',1);		
		$query = $this->db->get('tbl_news');
		if($query->num_rows() >0)
		{
			$data = $query->row_array();
			$data = $this->get_newscontinue($data);
			$data = $this->get_namecat2($data);
			return $data;
		}
		return false;
	}
	public function get_namecat2($data)
	{
		if(!empty($data))
		{
				$this->db->where("cat_id",$item['cat_id']);
				$data['name_cat'] = $this->db->get("tbl_news_cat")->row_array();
				return $data;
		}
	}
	public function get_newscontinue($data)
	{
		$this->db->where("cat_id",$data['cat_id']);
		$this->db->where("news_status",1);
		$this->db->order_by("news_id","DESC");
		$this->db->limit(6);
		$data["news_sub"] = $this->db->get('tbl_news')->result_array();
		return $data;
	}
		/*** index **///
	
	public function get_data($lang,$num='',$off='')
	{
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
				$this->db->limit($off, $num);
				$this->db->where('news_lang =',$lang);
				$this->db->where('news_status =',1);
				$this->db->join("tbl_news_cat","tbl_news_cat.cat_id = cat_id");
				$this->db->order_by($this->pri_key,"DESC");
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	public function get_news_cat($catid,$num,$off)
	{
			$this->db->where('cat_id',$catid);
			$this->db->where('news_status',1);
			$this->db->limit($off,$num);
			$query = $this->db->get('tbl_news');
			if($query->num_rows() >0)
			{
				$data = $query->result_array();
				
				//$data = $this->get_namecat();
				return $data;
			}
	
		return false;
	}
	
	public function get_namecat($data)
	{
		if(!empty($data))
		{
			foreach($data as $item)
			{
				$this->db->where("cat_id",$item['cat_id']);
				$subdata = $this->db->get("tbl_news_cat")->row_array();
			}	
			return $subdata;
		}
	}
	public function get_namecat_detail($data)
	{
		$this->db->where("cat_id",$data['cat_id']);
		$subdata = $this->db->get("tbl_news_cat")->row_array();
		return $subdata;

	}
	
	
	public function get_news_type($typeid,$num=1,$off=1)
	{
		
		$this->db->where('type_id',$typeid);
		$this->db->where('news_status',1);
		$this->db->limit($off,$num);
		$query = $this->db->get('tbl_news');
		if($query->num_rows() >0)
		{
			
			$data = $query->result_array();
			//$data = $this->get_nametype($data);
			//$data = $this->get_namecat($data);
			return $data;
		}
		return false;
	}
	public function get_nametype($data)
	{	
		if(!empty($data))
		{
			foreach($data as $item)
			{
				$this->db->where("type_id",$item['type_id']);
				$subdata = $this->db->get("tbl_news_type")->row_array();
			}	
			return $subdata;
		}
		
	}
	public function get_manu($lang)
	{
		$this->db->where('ori_lang',$lang);
		$query = $this->db->get('tbl_origin');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	
	public function news_from_cate($num,$off,$id,$dd="") 
	{
			$this->db->select('news.name,news.id,news.promotion,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->limit($off,$num);
		$this->db->where("sub_id =",$id);
			$this->db->join("unit" , "unit.id = unit" );
		if($dd != 0 && $dd != "")
		{
		$this->db->where("dd".$dd." =",1);
		}$this->db->order_by($this->table.".".$this->pri_key, "desc"); 
		return $query =$this->db->get($this->table)->result_array();
	}
		public function del_item($id)
	{
		$this->db->where($this->pri_key, $id);
		$this->db->delete($this->table); 
			
	}
	
	

	public function get_cate()
	{
		return $query = $this->db->get($this->table_cate)->result_array();
	}
	
	public function get_unit()
	{
	return $this->db->get("unit")->result_array();
	}
	
	
	function list_data_news_cat($num,$off,$cat)
	{
		$cat = (int)$cat;
		$this->db->limit($off,$num);
	
		$this->db->join("tbl_news_cat as a" , "a.cat_id = cat_id" );
		
			
		$this->db->order_by($this->pri_key, "desc"); 

		$query = $this->db->get($this->table);
		if($query->num_rows()  >0)
			{
			
					
				
				return $query->result_array();
			}
		return false;
	}
	function list_data($catID,$num,$off)
	{
		$this->db->limit($off,$num);
		if($this->db->count_all($this->table) >0)
			{
				$this->db->where("cat_id",$catID);
				$this->db->order_by($this->pri_key, "desc"); 
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	function insert_news($data)
	{
		
		if($this->db->insert($this->table,$data))
			return true;
		return false;
	
	}
	//***************** update news category **********************//
	function update_news($data,$id)
	{
		if(is_numeric($id) && $id !='')
		{
			
			$this->db->where($this->pri_key,$id);
			if($this->db->update($this->table,$data))
			{
				return true;
			}
			return false;
			
		}
		return false;
	}
	//****************** delete *******************************//
	function delete()
	{
		if (isset($_POST['checkbox1']))
		{
			foreach($_POST['checkbox1'] as $id) 
			{
				$row = $this->info_from_id($id,$this->table);
				$row = $row[0];
				if (!empty($row['news_image'])) unlink("./upload/news/".$row['news_image']);

				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 

			}			

		}

	}
	public function get_last_id()
	{
		$this->db->order_by($this->pri_key." DESC");
		
		return $this->db->get($this->table)->row_array();
		
		
	}
	//get news from cate
	public function get_news_from_cate($num,$off,$id)
	{
	$this->db->select('sub_categories.*,news.*,categories.*,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("categories.cate_id",$id);
		$this->db->join("sub_categories","sub_categories.sub_cate_id = news.sub_id");
	$this->db->join("categories","categories.cate_id = sub_categories.cate_id");
	$this->db->join("unit" , "unit.id = unit" );
	$this->db->limit($off,$num);
//	print_r($this->db->where());
	$query =$this->db->get("news");
	if($query->num_rows() >0)
	{
		return $query->result_array();
	}
	
	else
	return false;
	
	}
	public function count_news_from_cate($id)
	{
	//$this->db->select('sub_categories.*,news.*,categories.*,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("cat_id",$id);
///		$this->db->join("sub_categories","sub_categories.sub_cate_id = news.sub_id");
	$this->db->join("tbl_news_cat","cat_id = cat_id");
	
	$query =$this->db->get("tbl_news");
	
		return $query->num_rows();

	
	}
	function info_from_cat($catid,$lang =1)
	{
		if(is_numeric($catid) && $catid !="")
		{
			$where = array('cat_id' => $catid,'cat_lang'=>$lang);
			$this->db->where($where);
			
		//	$this->db->join("tbl_news_cat" , "cat_id = cat_id" );

	//		print_r($this->db->where());
			$query =$this->db->get('tbl_news_cat'); 
			return $query->result_array();
		}
		else
			return false;
	
	}
	
	// get info from id 
	public function info_from_id($id,$table)
	{
		if($table == 'tbl_news_cat')
		{
			$pri_key=$this->pri_key;
		}
	
		if(is_numeric($id) && $id !="")
		{
			$where = array($this->table.".".$this->pri_key => $id);
			$this->db->where($where);
			
		//	$this->db->join("tbl_news_cat" , "cat_id = cat_id" );

	//		print_r($this->db->where());
			$query =$this->db->get($table); 
			return $query->result_array();
		}
		else
			return false;
	}
	public function count_event($type)
	{
		$this->db->select('*')->from($this->table)->where($type,1);	
			$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );	
			
		$query = $this->db->get();
		return $query->num_rows();
	}
	
	public function get_event($type,$num='',$off='')
	{
		

		$this->db->select('news.name,news.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name')->from($this->table)->where($type,1)->limit($off, $num);
			$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );	
		$this->db->order_by($this->table.".".$this->pri_key." desc");
		$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	
	}
	function others_news($cateid,$id)
	{
		$this->db->select('sub_categories.*,news.*,news.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("sub_id",$cateid);
		$this->db->where("news.id != ".$id);
		$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
			$this->db->join("unit" , "unit.id = unit" );
		$this->db->limit(10);
		$this->db->order_by($this->table.".".$this->pri_key." desc");
		$query = $this->db->get($this->table);
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	}

	//dem so record cua sub id
	function count_news_sub($sub)
	{
		$this->db->where("news.sub_id",$sub);
		$this->db->join("sub_categories" , "sub_cate_id = news.sub_id" );
		$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );
		$query=  $this->db->get($this->table);
		return $query->num_rows() ;
	}
	// dem so record 
	function count_all($catID,$table)
	{
		$this->db->where("cat_id",$catID);
		return $this->db->count_all_results($table);
		
	}
	function count_all_type($typeID,$table)
	{
		$this->db->where("type_id",$typeID);
		return $this->db->count_all_results($table);
		
	}

	
		function count_by_cate($cate)
	{
		$this->db->where("sub_id",$cate);
		
		$this->db->join("unit" , "unit.id = unit" );	
		$query = $this->db->get($this->table);
		return $query->num_rows();
	}
	
	function search ($keyword , $type,$num='',$off='')
	{
		//$this->db->select('*')->from($this->table)->where($type,)->limit($off, $num);
		$this->db->select('news.*,news.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		if($type !=0)
		{
			$this->db->where("sub_id",$type);
		}
			$this->db->like("news.name",$keyword);
	$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
			$this->db->join("unit" , "unit.id = unit" );
			$this->db->limit($off, $num);
		$this->db->order_by($this->pri_key." desc");
		$query= $this->db->get($this->table);
		//print_r($this->db->where());
		//$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
		
	}

	
	function insert_order($data)
	{
		if($this->db->insert('tbl_order',$data))
			return true;
		return false;
	
	}
 

}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>